using AutoMapper;
using B.S.NewMedical.Api.Write.Application.Command.ContentManagement.Carousel;
using B.S.NewMedical.Domain.Hospital;
using B.S.NewMedical.Infrastructure.Interface;
using MediatR;

namespace B.S.NewMedical.Api.Write.Application.Handler.ContentManagement.Carousel
{
    /// <summary>
    /// 新增轮播图命令处理器
    /// </summary>
    public class AddCarouselCommandHandler : IRequestHandler<AddCarouselCommand, int>
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<CarouselModel> _carouselRepository;

        public AddCarouselCommandHandler(IMapper mapper, IBaseRepository<CarouselModel> carouselRepository)
        {
            _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
            _carouselRepository = carouselRepository ?? throw new ArgumentNullException(nameof(carouselRepository));
        }

        /// <summary>
        /// 处理新增轮播图命令
        /// </summary>
        /// <param name="request">新增轮播图命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>新增的轮播图ID</returns>
        public async Task<int> Handle(AddCarouselCommand request, CancellationToken cancellationToken)
        {
            // 使用 AutoMapper 将命令映射为实体
            var carouselEntity = _mapper.Map<CarouselModel>(request);

            // 设置创建时间
            carouselEntity.CreateTime = DateTime.Now;

            // 调用仓储层保存数据
            await _carouselRepository.CreateAsync(carouselEntity);
            return carouselEntity.Id;
        }
    }
}